package com.hbean.service.impl;

import com.hbean.entity.SysDic;
import com.hbean.mapper.SysDicMapper;
import com.hbean.service.SysDicService;
import com.hbean.system.config.SysCache;
import com.hbean.utils.Condition;
import com.hbean.utils.HUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-09-29 00:41
 * @ModificationHistory:
 */

@Service
public class SysDicServiceImpl implements SysDicService {

    @Autowired
    private SysDicMapper sysDicMapper;
    @Autowired
    private SysCache sysCache;

    @Override
    @CachePut(value = "sysCache", key="'sysDic'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysDic> insertDataAndUpdateCache(SysDic sysDic) {

        sysDic.setSd03(sysDic.getSd02());//父级节点编号
        sysDic.setSd04(sysCache.getSysDicNameByID(sysDic.getSd02()));
        sysDic.setSd00(HUtil.uuidOf18());//主键
        sysDic.setSd02(HUtil.uuidOf18());//唯一标识

        this.sysDicMapper.insertData(sysDic);

        return this.sysDicMapper.selectDatas(new Condition());
    }

    @Override
    @CachePut(value = "sysCache", key="'sysDic'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysDic> deleteDataAndUpdateCache(SysDic sysDic) {

        this.sysDicMapper.deleteData(sysDic);

        return this.sysDicMapper.selectDatas(new Condition());
    }

    @Override
    @CachePut(value = "sysCache", key="'sysDic'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysDic> updateDataAndUpdateCache(SysDic sysDic) {

        this.sysDicMapper.updateData(sysDic);

        return this.sysDicMapper.selectDatas(new Condition());
    }

    @Override
    @Cacheable(value = "sysCache", key="'sysDic'") //开启缓存策略
    public List<SysDic> getDataForCache() {
        return this.sysDicMapper.selectDatas(new Condition());
    }

}
